<template>
  <div>
    <el-dialog
      title="设备添加"
      :visible.sync="dialogVisible"
      center
      width="40%"
      :before-close="handleClose">
      <el-card >
        <el-form :model="group" status-icon :rules="rules" ref="group" label-width="100px">
          <el-form-item label="名称" prop="fname"  required>
            <el-input v-model="group.fname" autocomplete="off" required></el-input>
          </el-form-item>
          <el-form-item label="编码" prop="fnumber" required>
            <el-input v-model="group.fnumber" autocomplete="off" required></el-input>
          </el-form-item>
          <el-form-item label="上级分类" prop="fparentid" id="fparentid">
            <el-select v-model="group.fparentid"  autocomplete="off"  filterable placeholder="请选择">
              <el-option v-for="item in groupList" :key="item.nodeId+''" :label="item.nodeName" :value="item.nodeId+''"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button @click=" handleClose()">关闭</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import Group from "@/api/psyManage/Group";

export default {
  name: "add_group",
  props: {
    groupAdd:Object,
    groupId:''
  },
  data() {
    return {
      isLoading:true,
      dialogVisible: true,
      group: {
        name: '',
        fnumber:'',
        fparentid:''
      },
      groupList:'',
      dataManager: {},
      rules: {
        fname: [{required: true, message: '名称不能为空', trigger: 'blur'}],
        fnumber: [{required: true, message: '编码不能为空', trigger: 'blur'}],
      }
    }
  },
  async created() {
    this.isLoading = true;
    let api = new Group();
    let groupList = await api.queryGroupTree();
    this.groupList=groupList.data;
    debugger
    console.log(groupList)
  },

  methods: {

    handleClose() {
      this.$emit("close", {name: 'add', isRefresh:false}
      );
    },

    //提交
    submitForm() {
      this.$refs['group'].validate((valid) => {
        if (valid) {
          const loading = this._loading();
          const api = new Group();
          api.add(this.group).then(res => {
            loading.close();
            this.$tie.showSuccess(res.message)
            this.$emit("close", {name: 'add', isRefresh: true});
          }).catch(e => loading.close())
        }
      })

    },
    //加载提示
    _loading() {
      return this.$loading({
        lock: true,
        text: '请耐心等待.....',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
    }
  }

}

</script>

<style scoped>

</style>
